package com.cencat.framework.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cencat.framework.core.dto.PageQuery;
import com.cencat.framework.core.dto.PageResult;
import com.cencat.framework.core.entity.BaseEntity;
import com.cencat.framework.core.mapper.BaseMapper;
import com.cencat.framework.core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
    
    @Autowired
    protected M baseMapper;
    
    @Override
    public PageResult<T> page(PageQuery pageQuery) {
        IPage<T> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<T> result = baseMapper.selectPage(page, null);
        return PageResult.of(result);
    }
    
    @Override
    public PageResult<T> page(PageQuery pageQuery, Wrapper<T> queryWrapper) {
        IPage<T> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<T> result = baseMapper.selectPage(page, queryWrapper);
        return PageResult.of(result);
    }
    
    @Override
    public List<T> listByCondition(Wrapper<T> queryWrapper) {
        return baseMapper.selectList(queryWrapper);
    }
}